﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Framework;
using System.Data;
using System.Threading;

namespace PICUpdate
{
    public class BIZUpdate
    {
        private IShowMessage m_IShowMessage = null;

        private string myServerAddress = "";
        private string myDataBase = "";
        private string myUsername = "";
        private string myPassword = "";
        private string localFilePath = "";
        private string upUrl = "";

        private bool m_running = true;

        public BIZUpdate()
        {
        }

        public void Start(string myServerAddress, string myDataBase, string myUsername, string myPassword,
            string localFilePath, string upUrl, IShowMessage iShowMessage)
        {
            this.m_IShowMessage = iShowMessage;
            this.myServerAddress = myServerAddress;
            this.myDataBase = myDataBase;
            this.myUsername = myUsername;
            this.myPassword = myPassword;
            this.localFilePath = localFilePath;
            this.upUrl = upUrl;
            Thread thread = new Thread(new ThreadStart(this.Exec));
            thread.IsBackground = true;
            thread.Start();
        }

        public void Stop()
        {
            this.m_running = false;
        }

        private void Exec()
        {

            while (this.m_running == true)
            {
                string sqlConfig = "Data Source={0};Initial Catalog={1};User Id={2};Password={3}";

                sqlConfig = string.Format(sqlConfig, myServerAddress, myDataBase, myUsername, myPassword);
                SqlHelper sqlHelper = new SqlHelper(sqlConfig);
                DataTable dt = sqlHelper.ExecuteDataTable("select * from cp.ParkInfoSetting");
                ParkInfoSetting parkInfoSetting = new ParkInfoSetting();
                if (dt.Rows.Count > 0)
                {

                    parkInfoSetting.instid = dt.Rows[0]["instid"].ToString();
                    parkInfoSetting.mchntid = dt.Rows[0]["mchntid"].ToString();
                }

                DataTable dt2 = sqlHelper.ExecuteDataTable("select top 5 * from cp.UpPicture where IsUp = 0 and DATEDIFF(SECOND,opttime,getdate())>10");
                List<UpPicture> listUpPicture = new List<UpPicture>();
                if (dt2.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt2.Rows)
                    {
                        UpPicture upPicture = new UpPicture();
                        upPicture.OldPicName = dr["OldPicName"].ToString();
                        upPicture.NewPicName = dr["NewPicName"].ToString();
                        upPicture.OptTime = DateTime.Parse(dr["OptTime"].ToString());
                        upPicture.IsUp = bool.Parse(dr["IsUp"].ToString());
                        listUpPicture.Add(upPicture);
                    }
                }

                foreach (UpPicture upPicture in listUpPicture)
                {
                    char[] charArray = new char[] { ';' };
                    string[] strArray = upPicture.OldPicName.Split(charArray, StringSplitOptions.RemoveEmptyEntries);
                    if (strArray.Count() == 0)
                    {
                        string strSql = "update  cp.UpPicture set IsUp = 1 where OptTime = '{0}'";
                        strSql = string.Format(strSql, upPicture.OptTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        sqlHelper.ExecuteNonQuery(strSql);
                        continue;
                    }
                    string strToSign = parkInfoSetting.instid + parkInfoSetting.mchntid + upPicture.NewPicName;

                    string strAfterSign = "";

                    bool bol = ToolsFromJY.Sha1WithRSAHelper.Sign(strToSign, out strAfterSign);
                    if (bol == false)
                    {
                        continue;
                    }

                    if (this.m_IShowMessage != null)
                    {
                        this.m_IShowMessage.ShowUpdateFile(localFilePath + strArray[0]);

                    }
                    bol = Framework.JYFileUpdate.UpdatePicture(upUrl, parkInfoSetting.instid, parkInfoSetting.mchntid, upPicture.NewPicName.Substring(0, 8),
                        upPicture.NewPicName, localFilePath + strArray[0], strAfterSign);

                    if ((DateTime.Now - upPicture.OptTime).TotalMinutes > 5 || bol == true)
                    {
                        string strSql = "update  cp.UpPicture set IsUp = 1 where OptTime = '{0}'";
                        strSql = string.Format(strSql, upPicture.OptTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        sqlHelper.ExecuteNonQuery(strSql);
                    }

                    Thread.Sleep(10);

                }
                Thread.Sleep(1000);
            }
        }
    }
}
